package com.example.result.explore

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.PointerEventPass
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.dp
import com.example.result.ui.theme.AppTheme

@Composable
fun PrintPointerInputEvent() {
    AppTheme {
        var pointers by remember {
            mutableStateOf("")
        }
        var needIntercept by remember {
            mutableStateOf(false)
        }
        var needIntercept2 by remember {
            mutableStateOf(false)
        }
        var needIntercept3 by remember {
            mutableStateOf(false)
        }

        LazyColumn (modifier = Modifier.fillMaxHeight()){
            item {
                Text(text = "打印pointerInputEvent")
            }
            item {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Button(onClick = { pointers = "" }) {
                        Text(text = "清空")
                    }
                    Row {
                        Text(text = "初始化父消费")
                        Switch(
                            checked = needIntercept,
                            onCheckedChange = {
                                needIntercept = it
                            }
                        )
                    }
                    Row {
                        Text(text = "主流程父消费")
                        Switch(
                            checked = needIntercept2,
                            onCheckedChange = {
                                needIntercept2 = it
                            }
                        )
                    }
                    Row {
                        Text(text = "终止流程父消费")
                        Switch(
                            checked = needIntercept3,
                            onCheckedChange = {
                                needIntercept3 = it
                            }
                        )
                    }

                }

            }
            item {
                Box(
                    modifier = Modifier
                        .background(MaterialTheme.colorScheme.onSurface)
                        .fillMaxWidth()
                        .height(40.dp)
                        .pointerInput(needIntercept) {
                            awaitPointerEventScope {
                                while (true) {
                                    awaitPointerEvent(PointerEventPass.Initial).apply {
                                        pointers += "\n父-initial:" + if (!changes.any { it.isConsumed }) {
                                            type
                                        } else {
                                            "被消费"
                                        }

                                        if (needIntercept) {
                                            changes.forEach {
                                                it.consume()
                                            }
                                        }
                                    }
                                    awaitPointerEvent(PointerEventPass.Main).apply {
                                        pointers += "\n父-main:" + if (!changes.any { it.isConsumed }) {
                                            type
                                        } else {
                                            "被消费"
                                        }
                                        if (needIntercept2) {
                                            changes.forEach { it.consume() }
                                        }
                                    }
                                    awaitPointerEvent(PointerEventPass.Final).apply {
                                        pointers += "\n父-final:" + if (!changes.any { it.isConsumed }) {
                                            type
                                        } else {
                                            "被消费"
                                        }
                                        if (needIntercept3) {
                                            changes.forEach { it.consume() }
                                        }
                                    }
                                }
                            }
                        }
                ) {
                    Box(
                        modifier = Modifier
                            .background(MaterialTheme.colorScheme.inverseOnSurface)
                            .size(20.dp)
                            .align(Alignment.Center)
                            .pointerInput(needIntercept) {
                                awaitPointerEventScope {
                                    while (true) {

                                        awaitPointerEvent(PointerEventPass.Initial).apply {
                                            pointers += if (!changes.any { it.isConsumed }) {
                                                "\n\t子-initial:$type"
                                            } else {
                                                "\n\t子-initial:被消费"
                                            }
                                        }
                                        awaitPointerEvent(PointerEventPass.Main).apply {
                                            pointers += if (!changes.any { it.isConsumed }) {
                                                "\n\t子-main:$type"
                                            } else {
                                                "\n\t子-main:被消费"
                                            }

                                        }
                                        awaitPointerEvent(PointerEventPass.Final).apply {
                                            pointers += if (!changes.any { it.isConsumed }) {
                                                "\n\t子-final:$type"
                                            } else {
                                                "\n\t子-final:被消费"
                                            }
                                            pointers += "\n"
                                        }
                                    }
                                }
                            }

                    )
                }
            }
            item {
                Text(text = pointers, modifier = Modifier.fillMaxWidth())
            }
        }
    }
}